<template>
    <div>
        <h1>添加优惠卷</h1>
        <table class="table table-bordered" border="1px">
            <tbody>
                <tr>
                    <td>优惠卷类型</td>
                    <td>
                        <select v-model="info.Distype">
                            <option :value="0">请选择</option>
                            <option :value="1">代金券</option>
                            <option :value="2">折扣券</option>
                            <option :value="3">兑换卷</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>优惠卷标题</td>
                    <td>
                        <input type="text" v-model="info.Title">
                    </td>
                </tr>
                <tr>
                    <td>优惠卷副标题</td>
                    <td>
                        <input type="text" v-model="info.SubTitle">
                    </td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                        <input type="file" @change="img">
                        <img :src="info.Photo" width="120px" height="100px" v-if="info.Photo != ''">
                    </td>
                </tr>
                <tr>
                    <td>库存</td>
                    <td>
                        <input type="text" v-model="info.Count">
                    </td>
                </tr>
                <tr>
                    <td>限领个数</td>
                    <td>
                        <input type="text" value="1" v-model="info.PeopleCount" disabled>
                    </td>
                </tr>
                <tr>
                    <td>使用条件</td>
                    <td>
                        <input type="text" v-model="info.PayMoney">
                    </td>
                </tr>
                <tr v-if="info.Distype == 1 ">
                    <td>减免价格</td>
                    <td>
                        <input type="text" v-model="info.discountprice" >
                    </td> 
                </tr>
                <tr v-if="info.Distype == 2 ">
                    <td>优惠价格</td>
                    <td>
                        <input type="text" v-model="info.Jianm" >
                    </td>
                </tr>
                <tr>
                    <td>有效时间</td>
                    <td>
                        <input type="date" v-model="info.BeginTime">-<input type="date" v-model="info.EndTime">
                    </td>
                </tr>
                <tr>
                    <td>适用业务</td>
                    <td>
                        <input type="checkbox" v-model="info.Service" value="订房">订房
                        <input type="checkbox" v-model="info.Service" value="商城">商城
                        <input type="checkbox" v-model="info.Service" value="堂食">堂食
                        <input type="checkbox" v-model="info.Service" value="付款码">付款码
                    </td>
                </tr>
                <tr>
                    <td>使用须知</td>
                    <td>
                        <textarea cols="50" rows="5" v-model="info.Notice"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>适用门店</td>
                    <td>
                        <select v-model="info.ShopName">
                            <option value="">请选择</option>
                            <option :value="a.StoreName" v-for="a in hh">{{ a.StoreName }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>服务电话</td>
                    <td>
                        <input type="text" maxlength="11" v-model="info.Phone">
                    </td>
                </tr>
                <tr>
                    <td>优惠卷颜色</td>
                    <td>
                        <input type="color" v-model="info.Color">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" value="1" v-model="info.State">发布
                        <input type="radio" value="0" v-model="info.State">不发布
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button value="提交" class="btn btn-primary" @click="ok">提交</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRouter } from 'vue-router'
const router = useRouter();

onMounted(() => {
    bind();
})

let hh: any = ref([])

let info: any = reactive({
    "Distype": 0,
    "Title": "",
    "SubTitle": "",
    "Photo": "",
    "Count": '',
    "PeopleCount": 1,
    "PayMoney": '',
    "BeginTime": "",
    "EndTime": "",
    "Service": [],
    "Notice": "",
    "ShopName": "",
    "Phone": "",
    "Color": "",
    "discountprice": 10,
    "Jianm": 0,
    "State": 1,
})

const bind = () => {
    axios({
        url: 'https://localhost:7056/api/Discount/Binds',
        method: 'get',
    })
        .then((res) => {
            Object.assign(hh.value, res.data);
        })
}

const img = (e: any) => {
    let obj = e.target.files[0];
    let f = new FormData;
    f.append('file', obj);
    axios({
        url: 'https://localhost:7056/img',
        method: 'post',
        data: f
    })
        .then((res) => {
            info.Photo = res.data
        })
}

const ok = () => {
    if (info.Distype == "") {
        alert('优惠卷类型不能为空!');
        return;
    }
    if (info.Title == "") {
        alert('优惠卷标题不能为空!');
        return;
    }
    if (info.SubTitle == "") {
        alert('优惠卷副标题不能为空!');
        return;
    }
    if (info.Photo == "") {
        alert('图片不能为空!');
        return;
    }
    if (info.Count == "") {
        alert('库存不能为空!');
        return;
    }
    if (info.PeopleCount == "") {
        alert('限领个数不能为空!');
        return;
    }
    if (info.PayMoney == "") {
        alert('使用条件不能为空!');
        return;
    }
    if (info.BeginTime == "") {
        alert('有效时间不能为空!');
        return;
    }
    if (info.EndTime == "") {
        alert('截至有效时间不能为空!');
        return;
    }
    if (info.Service == "") {
        alert('适用业务不能为空!');
        return;
    }
    if (info.Notice == "") {
        alert('使用须知不能为空!');
        return;
    }
    if (info.ShopName == "") {
        alert('适用门店不能为空!');
        return;
    }
    if (info.Phone == "") {
        alert('服务电话不能为空!');
        return;
    }
    if (info.Color == "") {
        alert('优惠卷颜色不能为空!');
        return;
    }
    if (info.State == "") {
        alert('状态不能为空!');
        return;
    }
    info.Service = info.Service.toString();
    axios({
        url: 'https://localhost:7056/api/Discount/AddDis',
        method: 'post',
        data: info
    })
        .then((res) => {
            if (res.data == -1) {
                alert('重复名称!');
                return;
            }
            else if (res.data > 0) {
                alert('添加成功！');
                router.push('/disashow')
                return;
            }
            else {
                alert('添加失败!');
                return;
            }
        })
}

</script>

<style scoped></style>